/**
 * @developer：MR·Feng
 * AES加密解密
 */

import * as CryptoJS from "crypto-js";

const AuthTokenKey = "cmbCampus0722000";

/* AES加密 */
function Encrypt(plaintText) {
    let key = CryptoJS.enc.Latin1.parse(AuthTokenKey);
    let options = {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7,
    };
    let encryptedData = CryptoJS.AES.encrypt(plaintText, key, options);
    let encryptedBase64Str = encryptedData.toString().replace(/\//g, "/");
    encryptedBase64Str = encryptedBase64Str.replace(/\+/g, "-");
    return encryptedBase64Str;
}

/* AES解密 */
function Decrypt(encryptedBase64Str) {
    let values = encryptedBase64Str.replace(/-/g, "+").replace(/_/g, "/");
    let key = CryptoJS.enc.Latin1.parse(AuthTokenKey);
    let options = {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7,
    };
    let decryptedData = CryptoJS.AES.decrypt(values, key, options);
    let decryptedStr = CryptoJS.enc.Utf8.stringify(decryptedData);
    return decryptedStr;
}

export const AES = {
    Encrypt,
    Decrypt,
};